package com.starwish.starwishblog.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.starwish.starwishblog.entity.Advice;
import com.starwish.starwishblog.entity.Body;
import com.starwish.starwishblog.mapper.AdviceMapper;
import com.starwish.starwishblog.mapper.BodyMapper;
import com.starwish.starwishblog.vo.AdviceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AdviceService {
    @Autowired
    AdviceMapper adviceMapper;

    @Autowired
    BodyMapper bodyMapper;
    @Transactional
    public int addAdvices(AdviceVo adviceVo) {
        //先添加body
        Body body = adviceVo.getBody();
        bodyMapper.insert(body);
        adviceVo.getBody().setId(body.getId());
        return adviceMapper.insert(adviceVo);
    }

    public int removeAdvice(int id) {
        return adviceMapper.deleteById(id);
    }

    public int cancelAdviceById(Integer id,Integer status) {
        return adviceMapper.cancelById(id,status);
    }

    public int topAdviceById(Integer id,Integer status) {
        return adviceMapper.topById(id,status);
    }


    @Transactional
    public int modify(AdviceVo adviceVo) {
        //修改内容
        bodyMapper.updateByPrimaryKey(adviceVo.getBody());
        return adviceMapper.updateByPrimaryKey(adviceVo);
    }

    public PageInfo<AdviceVo> queryAdviceByPage(Integer now, Integer size, String title) {
        PageHelper.startPage(now,size);
        List<AdviceVo> lists = adviceMapper.selectAdviceByCons(title);
        return new PageInfo<>(lists);
    }

    public PageInfo<AdviceVo> queryAdviceByWeight(Integer now, Integer size, String title) {
        PageHelper.startPage(now,size);
        List<AdviceVo> lists = adviceMapper.selectAdviceByWeight(title);
        return new PageInfo<>(lists);
    }

    public List<Body> queryBodyAll() {
        return adviceMapper.selectBodyAll();
    }


    public List<AdviceVo> findAllAdvices() {
        return adviceMapper.selectAdviceByImportance();
    }
}
